﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Text;
using MvcContrib.Pagination;

namespace Framework.Util.Helpers
{
    public static class ViewHelper
    {        
        public static string FormatTimeRelative(this HtmlHelper html, DateTime from, DateTime to)
        {
            string[] partial = { "", "", "", "", "" };
            var dt = to.Subtract(from);
            if (dt.Days / 30 > 0)
                partial[0] = string.Format("{0} Month", dt.Days / 30);
            if (dt.Days % 30 > 0)
                partial[1] = string.Format("{0} Days", dt.Days % 30);
            if (dt.Hours > 0)
                partial[2] = string.Format("{0} Hours", dt.Hours);
            if (dt.Minutes > 0)
                partial[3] = string.Format("{0} Minutes", dt.Minutes);
            if (dt.Seconds > 0)
                partial[4] = string.Format("{0} Seconds", dt.Seconds);
            var result = from n in partial
                         where n != ""
                         select n;
            var rs = string.Join(" ", result.Take<string>(2).ToArray<string>());
            return string.IsNullOrEmpty(rs) ? "1 giây" : rs;
        }

        public static string DefaultAvatar(this UrlHelper helper, string avatar)
        {
            return string.IsNullOrEmpty(avatar) ?
                helper.Content("~/Content/images/DefaultAvatar.jpg") :
                helper.Content("~/" + avatar);
        }

        public static string FormatUsername(this UrlHelper helper, string username, int? length)
        {
            if (string.IsNullOrEmpty(username))
                return "";
            var truncname = username;
            if (length.HasValue && length.Value < username.Length)
                truncname = username.Substring(0, length.Value);
            return string.Format("<a href=\"{0}\" title=\"{1}\">{2}</a>",
                helper.GetProfileUrl(username),
                username,
                truncname);
        }

        public static string GetProfileUrl(this UrlHelper helper, string username)
        {
            return string.IsNullOrEmpty(username) ?
                "" :
                helper.Content("~/Profile/" + username);
        }

        public static string Truncate(this HtmlHelper helper, string original, int length)
        {
            return Truncate(original, length, true, true);
        }

        public static string Truncate(string str, int length)
        {
            return Truncate(str, length, true, true);
        }

        public static string Truncate(string str, int length, bool wordsafe, bool dot)
        {
            if (str.Length <= length)
                return str;

            if (dot)
                length -= 4;

            if (wordsafe)
            {
                str = str.Substring(0, length + 1);
                var lastspace = str.LastIndexOf(' ');
                str = str.Substring(0, lastspace > 0 ? lastspace : length);
            }
            else
                str = str.Substring(0, length + 1);

            if (dot)
                str += " ...";
            return str;
        }

        public static string ConvertToAscii(this HtmlHelper helper, string input)
        {
            var stFormD = input.Normalize(NormalizationForm.FormD);
            var sb = new StringBuilder();
            foreach (var t in stFormD)
            {
                var uc = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(t);
                if (uc != System.Globalization.UnicodeCategory.NonSpacingMark)
                {
                    sb.Append(t);
                }
            }
            sb = sb.Replace('\u0111', 'd').Replace('\u0110', 'D');
            return (sb.ToString().Normalize(NormalizationForm.FormD));
        }

        public static string ConvertToAscii(string input)
        {
            var stFormD = input.Normalize(NormalizationForm.FormD);
            var sb = new StringBuilder();
            foreach (var t in stFormD)
            {
                var uc = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(t);
                if (uc != System.Globalization.UnicodeCategory.NonSpacingMark)
                {
                    sb.Append(t);
                }
            }
            sb = sb.Replace('\u0111', 'd').Replace('\u0110', 'D');
            return (sb.ToString().Normalize(NormalizationForm.FormD));
        }

        /// <summary>
        /// Creates a pager component using the specified IPagination as the datasource.
        /// </summary>
        /// <param name="helper">The HTML Helper</param>
        /// <param name="pagination">The datasource</param>
        /// <returns>A Pager component</returns>
        public static ExtPager Pager(this HtmlHelper helper, IPagination pagination)
        {
            return new ExtPager(pagination, helper.ViewContext.HttpContext.Request);
        }

        public static string Slip(this HtmlHelper helper,string stringSource)
        {
            var stringDestination=stringSource;
            stringDestination=stringDestination.TrimEnd('0');
            stringDestination=stringDestination.TrimEnd('.');
            return stringDestination;
        }
    }
}